subroutine write_to_hdf5()
  use logs
  use hdf5_wrapper
  use header_vars
  use param_module
  use myfiles
  use particle_data

  implicit none
  integer             :: snap_handle
  character(len=512)  :: fname

  write(logunit,*)'  -writing hdf5 file'
  call flush(logunit)

  fname = trim(adjustl(outdir))//'/TracedUnique.hdf5'
  !
  call hdf5_create_file(snap_handle,fname)
  !
  call hdf5_create_group(   snap_handle,'/Header/')
  call hdf5_write_attribute(snap_handle,'/Header/IC_Redshift',binary_ics_header%redshift)
  call hdf5_write_attribute(snap_handle,'/Header/BoxSize',binary_ics_header%boxsize)
  call hdf5_write_attribute(snap_handle,'/Header/Hubble_at_z',HubbleParam_a)
  call hdf5_write_attribute(snap_handle,'/Header/NumPart',NumPart_Unique)
  call hdf5_write_attribute(snap_handle,'/Header/OriginalSelection_x0',x0)
  call hdf5_write_attribute(snap_handle,'/Header/OriginalSelection_y0',y0)
  call hdf5_write_attribute(snap_handle,'/Header/OriginalSelection_z0',z0)
  call hdf5_write_attribute(snap_handle,'/Header/OriginalSelection_r0',r0)

  !
  if(NumPart_Unique(0) > 0) call hdf5_create_group(snap_handle,'/PartType0/')
  if(NumPart_Unique(1) > 0) call hdf5_create_group(snap_handle,'/PartType1/')
  if(NumPart_Unique(2) > 0) call hdf5_create_group(snap_handle,'/PartType2/')
  if(NumPart_Unique(3) > 0) call hdf5_create_group(snap_handle,'/PartType3/')
  if(NumPart_Unique(4) > 0) call hdf5_create_group(snap_handle,'/PartType4/')
  if(NumPart_Unique(5) > 0) call hdf5_create_group(snap_handle,'/PartType5/')
  ! 
  if(NumPart_Unique(0) > 0) call hdf5_write_data(snap_handle,'/PartType0/ParticleIDs',UniqueIDs%Type0(0:NumPart_Unique(0)-1))
  if(NumPart_Unique(1) > 0) call hdf5_write_data(snap_handle,'/PartType1/ParticleIDs',UniqueIDs%Type1(0:NumPart_Unique(1)-1))
  if(NumPart_Unique(2) > 0) call hdf5_write_data(snap_handle,'/PartType2/ParticleIDs',UniqueIDs%Type2(0:NumPart_Unique(2)-1))
  if(NumPart_Unique(3) > 0) call hdf5_write_data(snap_handle,'/PartType3/ParticleIDs',UniqueIDs%Type3(0:NumPart_Unique(3)-1))
  if(NumPart_Unique(4) > 0) call hdf5_write_data(snap_handle,'/PartType4/ParticleIDs',UniqueIDs%Type4(0:NumPart_Unique(4)-1))
  if(NumPart_Unique(5) > 0) call hdf5_write_data(snap_handle,'/PartType5/ParticleIDs',UniqueIDs%Type5(0:NumPart_Unique(5)-1))
  !
  if(NumPart_Unique(0) > 0) &
       call hdf5_write_data(snap_handle,'/PartType0/LatestSelectionSnapshot',UniqueSnapIDs%Type0(0:NumPart_Unique(0)-1))
  if(NumPart_Unique(1) > 0) &
       call hdf5_write_data(snap_handle,'/PartType1/LatestSelectionSnapshot',UniqueSnapIDs%Type1(0:NumPart_Unique(1)-1))
  if(NumPart_Unique(2) > 0) &
       call hdf5_write_data(snap_handle,'/PartType2/LatestSelectionSnapshot',UniqueSnapIDs%Type2(0:NumPart_Unique(2)-1))
  if(NumPart_Unique(3) > 0) &
       call hdf5_write_data(snap_handle,'/PartType3/LatestSelectionSnapshot',UniqueSnapIDs%Type3(0:NumPart_Unique(3)-1))
  if(NumPart_Unique(4) > 0) &
       call hdf5_write_data(snap_handle,'/PartType4/LatestSelectionSnapshot',UniqueSnapIDs%Type4(0:NumPart_Unique(4)-1))
  if(NumPart_Unique(5) > 0) &
       call hdf5_write_data(snap_handle,'/PartType5/LatestSelectionSnapshot',UniqueSnapIDs%Type5(0:NumPart_Unique(5)-1))
  !                      
  if(NumPart_Unique(0) > 0) &
       call hdf5_write_data(snap_handle,'/PartType0/IC_Coordinates',UniqueCoordinates%Type0(0:2,0:NumPart_Unique(0)-1))
  if(NumPart_Unique(1) > 0) &
       call hdf5_write_data(snap_handle,'/PartType1/IC_Coordinates',UniqueCoordinates%Type1(0:2,0:NumPart_Unique(1)-1))
  if(NumPart_Unique(2) > 0) &
       call hdf5_write_data(snap_handle,'/PartType2/IC_Coordinates',UniqueCoordinates%Type2(0:2,0:NumPart_Unique(2)-1))
  if(NumPart_Unique(3) > 0) &
       call hdf5_write_data(snap_handle,'/PartType3/IC_Coordinates',UniqueCoordinates%Type3(0:2,0:NumPart_Unique(3)-1))
  if(NumPart_Unique(4) > 0) &
       call hdf5_write_data(snap_handle,'/PartType4/IC_Coordinates',UniqueCoordinates%Type4(0:2,0:NumPart_Unique(4)-1))
  if(NumPart_Unique(5) > 0) &
       call hdf5_write_data(snap_handle,'/PartType5/IC_Coordinates',UniqueCoordinates%Type5(0:2,0:NumPart_Unique(5)-1))
  !                      
  if(NumPart_Unique(0) > 0) &
       call hdf5_write_data(snap_handle,'/PartType0/IC_Velocity',UniqueVelocity%Type0(0:2,0:NumPart_Unique(0)-1))
  if(NumPart_Unique(1) > 0) &
       call hdf5_write_data(snap_handle,'/PartType1/IC_Velocity',UniqueVelocity%Type1(0:2,0:NumPart_Unique(1)-1))
  if(NumPart_Unique(2) > 0) &
       call hdf5_write_data(snap_handle,'/PartType2/IC_Velocity',UniqueVelocity%Type2(0:2,0:NumPart_Unique(2)-1))
  if(NumPart_Unique(3) > 0) &
       call hdf5_write_data(snap_handle,'/PartType3/IC_Velocity',UniqueVelocity%Type3(0:2,0:NumPart_Unique(3)-1))
  if(NumPart_Unique(4) > 0) &
       call hdf5_write_data(snap_handle,'/PartType4/IC_Velocity',UniqueVelocity%Type4(0:2,0:NumPart_Unique(4)-1))
  if(NumPart_Unique(5) > 0) &
       call hdf5_write_data(snap_handle,'/PartType5/IC_Velocity',UniqueVelocity%Type5(0:2,0:NumPart_Unique(5)-1))
  !                      
  if(NumPart_Unique(0) > 0) &
       call hdf5_write_data(snap_handle,'/PartType0/ForceFree_Coordinates',ForceFreeCoordinates%Type0(0:2,0:NumPart_Unique(0)-1))
  if(NumPart_Unique(1) > 0) &
       call hdf5_write_data(snap_handle,'/PartType1/ForceFree_Coordinates',ForceFreeCoordinates%Type1(0:2,0:NumPart_Unique(1)-1))
  if(NumPart_Unique(2) > 0) &
       call hdf5_write_data(snap_handle,'/PartType2/ForceFree_Coordinates',ForceFreeCoordinates%Type2(0:2,0:NumPart_Unique(2)-1))
  if(NumPart_Unique(3) > 0) &
       call hdf5_write_data(snap_handle,'/PartType3/ForceFree_Coordinates',ForceFreeCoordinates%Type3(0:2,0:NumPart_Unique(3)-1))
  if(NumPart_Unique(4) > 0) &
       call hdf5_write_data(snap_handle,'/PartType4/ForceFree_Coordinates',ForceFreeCoordinates%Type4(0:2,0:NumPart_Unique(4)-1))
  if(NumPart_Unique(5) > 0) &
       call hdf5_write_data(snap_handle,'/PartType5/ForceFree_Coordinates',ForceFreeCoordinates%Type5(0:2,0:NumPart_Unique(5)-1))
  !
  call hdf5_close_file(snap_handle)

#ifdef BINARY_LIST
  ! Also dump binary table for Adrian
  write(logunit,*)'  -writing binary file for Adrian'
  call flush(logunit)
  write(logunit,*)'  '//trim(adjustl(fname))
  call flush(logunit)
  fname = trim(adjustl(outdir))//'/TracedUnique.bin'
  open(10,file=fname,form='unformatted',status='unknown')
  write(10)NumPart_Unique(0:5)
  write(10)binary_ics_header%redshift
  write(10)binary_ics_header%boxsize
  write(10)x0  ! <- added for Ali's TRAPHIC
  write(10)y0  ! <- added for Ali's TRAPHIC
  write(10)z0  ! <- added for Ali's TRAPHIC
  write(10)r0  ! <- added for Ali's TRAPHIC
  write(10)ForceFreeCoordinates%Type0(0:2,0:NumPart_Unique(0)-1)
  write(10)ForceFreeCoordinates%Type1(0:2,0:NumPart_Unique(1)-1)
  write(10)ForceFreeCoordinates%Type2(0:2,0:NumPart_Unique(2)-1)
  write(10)ForceFreeCoordinates%Type3(0:2,0:NumPart_Unique(3)-1)
  write(10)ForceFreeCoordinates%Type4(0:2,0:NumPart_Unique(4)-1)
  write(10)ForceFreeCoordinates%Type5(0:2,0:NumPart_Unique(5)-1)
  write(10)UniqueCoordinates%Type0(0:2,0:NumPart_Unique(0)-1)
  write(10)UniqueCoordinates%Type1(0:2,0:NumPart_Unique(1)-1)
  write(10)UniqueCoordinates%Type2(0:2,0:NumPart_Unique(2)-1)
  write(10)UniqueCoordinates%Type3(0:2,0:NumPart_Unique(3)-1)
  write(10)UniqueCoordinates%Type4(0:2,0:NumPart_Unique(4)-1)
  write(10)UniqueCoordinates%Type5(0:2,0:NumPart_Unique(5)-1)
  write(10)UniqueVelocity%Type0(0:2,0:NumPart_Unique(0)-1)
  write(10)UniqueVelocity%Type1(0:2,0:NumPart_Unique(1)-1)
  write(10)UniqueVelocity%Type2(0:2,0:NumPart_Unique(2)-1)
  write(10)UniqueVelocity%Type3(0:2,0:NumPart_Unique(3)-1)
  write(10)UniqueVelocity%Type4(0:2,0:NumPart_Unique(4)-1)
  write(10)UniqueVelocity%Type5(0:2,0:NumPart_Unique(5)-1)
  close(10)

#endif


end subroutine write_to_hdf5
